import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-calibration-ratio-total',
	templateUrl: './calibration-ratio-total.component.html',
	styleUrls: ['./calibration-ratio-total.component.css']
})
export class CalibrationRatioTotalComponent implements OnInit {
	tableData = []; //table数据
	resultTableData:any = [];
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	startTime_table:string = '';
	endTime_table:string = '';
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	carNumber:string = '';
	deviceId:string = '';
	timeType:string='1'; //时间类型
	jumpData:any; // 跳转页面接收到的数据
    initDateRange: any = {
        startTime:'',
        endTime:'',
    }// 时间组件需要渲染的值
    carDeatailUseList = carTypeListObj.carDeatailUseList
	constructor(private jsonApiService: JsonApiService, private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}

	ngOnInit() {
		this.load();
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time){
		this.startTime_table = time.startTime;
		this.endTime_table = time.endTime;
	} 
	//公司选中回调
    companySelected(data){
        this.selectedCompanyId = data.companyId;
        this.selectedCompanyName = data.companyName;
        this.deviceId = data.deviceId;
        this.carNumber = data.carNumber;
    }
	//表格搜索
	table_search() {
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//刷新
	refresh(){
    	this.load();
    }
    getUseTypeNameL2(key) {
        if(key) {
            return this.carDeatailUseList[key];
        } else {
            return '';
        }
    }
	//初始化加载list数据
	load() {
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				companyId: this.selectedCompanyId,
				deviceId: this.deviceId,
				startTime:this.startTime_table,
				endTime: this.endTime_table,
                searchKey: this.searchKey,
                type: '2',
                export: '0'
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("calibrationStatistics/statistics.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
            this.totalCount = data.recordCount;
            // this.client_pages(0,this.pageSize);
            for(let item of this.tableData) {
                item.useTypeL2 = this.getUseTypeNameL2(item.useTypeL2);
            }
		})
	}
	client_pages(start,length){
        this.resultTableData = this.tableData.slice(start,start+length);
        for(let item of this.resultTableData) {
            item.useTypeL2 = this.getUseTypeNameL2(item.useTypeL2);
        }
    }
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
		// this.client_pages(e.first,e.rows);
	}
	//导出报表
	exportData() {
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				companyId: this.selectedCompanyId,
				deviceId: this.deviceId,
				startTime:this.startTime_table,
				endTime: this.endTime_table,
                searchKey: this.searchKey,
                type: '2',
                export: '1'
			}
        }
		let timeCheck = common.correctTime(data.parameters, ['type', 'export'])
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(data, 'gridPager', config.BaseURL + 'calibrationStatistics/statistics.html')
	}
	// 页面跳转
    jumpPage(Url,row){
        let BaseURL = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
        common.setCookie('calibrationResult', JSON.stringify(row));
        window.open(BaseURL+'#'+Url);
    }
}